package com.wzh.mall.product.service.impl;

import com.wzh.mall.product.entity.SkuImagesEntity;
import com.wzh.mall.product.entity.SpuInfoDescEntity;
import com.wzh.mall.product.entity.SpuInfoEntity;
import com.wzh.mall.product.service.*;
import com.wzh.mall.product.vo.ItemSaleAttrsVo;
import com.wzh.mall.product.vo.SkuItemVo;
import com.wzh.mall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;

import com.wzh.mall.product.dao.SkuInfoDao;
import com.wzh.mall.product.entity.SkuInfoEntity;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {


    @Autowired
    SkuImagesService imagesService;

    @Autowired
    SpuInfoDescService spuInfoDescService;

    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();


        String key = (String)params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.and((w)->{
                 wrapper.eq("sku_id",key).or().like("sku_name",key);
            });
        }
        String catelogId = (String)params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId)&&!"0".equalsIgnoreCase(catelogId)){
            wrapper.eq("catalog_id",catelogId);
        }
        String brandId = (String)params.get("brandId");
        if (!StringUtils.isEmpty(brandId)&&!"0".equalsIgnoreCase(brandId)){
            wrapper.eq("brand_id",brandId);
        }
        String min = (String)params.get("min");
        if (!StringUtils.isEmpty(min)){
            try {
                BigDecimal bigDecimal = new BigDecimal(min);
                if (bigDecimal.compareTo(new BigDecimal("0"))==1){
                    wrapper.ge("price",min);
                }
            }catch (Exception e){

            }
        }
        String max = (String)params.get("max");
        if (!StringUtils.isEmpty(max)){
            try {
                BigDecimal bigDecimal = new BigDecimal(max);
                if (bigDecimal.compareTo(new BigDecimal("0"))==1){
                    wrapper.le("price",max);
                }
            }catch (Exception e){

            }

        }


        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
               wrapper
        );

        return new PageUtils(page);
    }


    @Override
    public List<SkuInfoEntity> getSkuBySpuId(Long spuId) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_id",spuId);
        List<SkuInfoEntity> list = this.list(wrapper);
        return list;
    }

    /**
      * @Description: 根据skuid获取到商品的详细信息
      * @Param: [skuId]
      * @return: com.wzh.mall.product.vo.SkuItemVo
      * @Author: Mr.Wang
      * @Date: 2021/9/13
      * @Time: 8:58
      */      
      
    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo vo = new SkuItemVo();

        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            // 1、sku的基本信息 -》pms_sku_info
            SkuInfoEntity info = getById(skuId);
            vo.setInfo(info);

            return info;
        }, executor);
        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((res) -> {
            // 3、获取到spu的销售属性组合
            List<ItemSaleAttrsVo> itemSaleAttrsVos = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
            vo.setSaleAttr(itemSaleAttrsVos);
        }, executor);

        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {
            // 4、获取spu的介绍
            // 先获取到这个sku所对应的spu的id
            SpuInfoDescEntity spuInfo = spuInfoDescService.getById(res.getSpuId());
            vo.setDesp(spuInfo);
        }, executor);

        CompletableFuture<Void> baseFuture = infoFuture.thenAcceptAsync((res) -> {
            // 5、获取spu的规格参数信息
            List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            vo.setGroupAttrVos(attrGroupVos);
        }, executor);


        CompletableFuture<Void> imgFuture = CompletableFuture.runAsync(() -> {
            // 2、sku的所有图片信息   pms_sku_images
            List<SkuImagesEntity> images = imagesService.getImagesBySkuId(skuId);
            vo.setImages(images);
        }, executor);

        // 等待所有任务都完成
        CompletableFuture.allOf(saleAttrFuture,descFuture,baseFuture,imgFuture).get();



        return vo;
    }

}